<%@page import="com.infolab.aims.domain.purchase.PurchaseOrder"%>
<%@ include file="/WEB-INF/views/taglib.jsp" %>
<h3>Purchase Order Form</h3>	
	<form:form id="form" method="post" modelAttribute="purchaseOrder"  cssClass="simpleForm purchaseOrderForm" >
		<form:hidden path="purchaseOrderId" />
		<form:hidden path="version" />
		<c:set var="newStatus" value="<%=PurchaseOrder.PURCHASEORDERSTATUS_NEW%>" />
		<c:set var="inprocessStatus" value="<%=PurchaseOrder.PURCHASEORDERSTATUS_INPROCESS%>" />
		<form:hidden path="purchaseOrderStatus" />
		<form:hidden path="accessDepartment.departmentId" />
		<form:hidden path="accessDepartment.departmentName" />
		<div class="action">
	  	<c:choose>
	  		<c:when test="${purchaseOrder.purchaseOrderId==null}">
	  			  			
	  			<button type="button" class="button positive" onclick="formSubmit()">
	  			Save
	  			</button>
	  			
	  			<a href="<c:url value="/purchaseOrder/create" />" class="button positive">Cancel</a>
	  			
	  			<a href="<c:url value="/purchaseOrder/search" />" class="button positive">Back</a>
	  		</c:when>
	  		<c:when test="${purchaseOrder.purchaseOrderId!=null}">
  				
  				<c:if test="${purchaseOrder.purchaseOrderStatus!=null && purchaseOrder.purchaseOrderStatus == newStatus}">
  				<button type="button" class="button positive" onclick="formUpdate()">
	  			Update
	  			</button>  
	  			</c:if>
	  			
	  			<c:if test="${purchaseOrder.purchaseOrderStatus!=null && ( purchaseOrder.purchaseOrderStatus == newStatus || purchaseOrder.purchaseOrderStatus == inprocessStatus )}">
	  			<a href="<c:url value="/purchaseOrder/close/${purchaseOrder.purchaseOrderId}" />" class="button positive">Close PO</a>
	  			</c:if>
	  			<a href="<c:url value="/purchaseOrder/print/${purchaseOrder.purchaseOrderId}" />" target="_blank" class="button">Print</a>
  				<button type="button" class="button positive" onclick="location.href='<c:url value="/purchaseOrder/search" />'">Back</button>
	  		</c:when>
	  	</c:choose>
	 	</div>	
	 	<div class="clear" style="height:1px">&nbsp;</div>
		<jsp:include page="/WEB-INF/views/message.jsp"></jsp:include>
		
		<c:choose>
		<c:when test="${purchaseOrder.purchaseOrderId==null}">
		<input name="flag" id="flag" type="hidden" value="false"/>	
		</c:when>

		<c:otherwise>
		<input name="flag" id="flag" type="hidden" value="true"/>	
		</c:otherwise>
		</c:choose>
		
	  	<fieldset>
	  		<legend>Purchase Order Info</legend>
	  		<div class="span-11">
				  		<aims:column cssErrorClass="error" path="purchaseOrderCode" >
				  		<form:label path="purchaseOrderCode">
				  			PO Code * 
				 		</form:label>
				  		<form:input path="purchaseOrderCode" /> <form:errors path="purchaseOrderCode"  cssClass="errorSimple" /> 
						</aims:column>
			
			
						<aims:column cssErrorClass="error" path="vendor" >
				  		<form:label path="vendor">
				  			Vendor 
				 		</form:label>	  	
				 			
				  		<form:select id="vendorId" path="vendor.vendorId" class="vendorId">
				 			<form:option value=""></form:option>
				 			<form:options items="${newVendorList}" itemValue="vendorId" itemLabel="vendorName" />
				 		</form:select>		  	
				  		
				  		<form:errors path="vendor"  cssClass="errorSimple" /> 
						</aims:column>
						
						<aims:column cssErrorClass="error" path="assetType" >
				  		<form:label path="assetType">
				  			Asset Type
				 		</form:label>	
				 		<form:select path="assetType" id="assetType">
				 			<form:options items="${globalAssetType}" itemValue="systemCode1" itemLabel="systemCodeValueDescription" />
				 		</form:select>
				  		 <form:errors path="assetType"  cssClass="errorSimple" />	 
						</aims:column>
						
						<aims:column cssErrorClass="error" path="location" >
				  		<form:label path="location">
				  			Receiving Location
				 		</form:label>
				  		<form:select id="locationId" path="location.locationId" class="locationId">
				 			<form:option value=""></form:option>
				 			<form:options items="${globalLocation}" itemValue="locationId" itemLabel="locationName" />
				 		</form:select>
				 		<form:errors path="location"  cssClass="errorSimple" /> 
						</aims:column>	
						
						<aims:column cssErrorClass="error" path="companyCode">
				  		<form:label path="companyCode">
				  			Company Code
				 		</form:label>	
				 		<form:select id="companyCode" path="companyCode" class="companyCode">
				 			<form:options items="${globalCompanyCode}" itemValue="systemCode1" itemLabel="systemCodeValueDescription" />
				 		</form:select>
				  		 <form:errors path="companyCode"  cssClass="errorSimple" />	 
						</aims:column>					
			
			</div>	
				
			<div class="span-11">
				
						<aims:column cssErrorClass="error" path="purchaseOrderDate" >
				  		<form:label path="purchaseOrderDate">
				  			PO Date  * 
				 		</form:label>
				  		<form:input path="purchaseOrderDate" id="poDate" onkeypress="return restrictCharacters(this, event, validChars);"/> 
				  		<input id="poDate_button" type="button" class="calendarBtn" />
				  		<form:errors path="purchaseOrderDate"  cssClass="errorSimple" /> 
						</aims:column>
						
						
						<aims:column cssErrorClass="error" path="purchaseOrderType" >
				  		<form:label path="purchaseOrderType">
				  			PO Order Type
				 		</form:label>
		  				
		  				<form:select id="purchaseOrderType" path="purchaseOrderType" class = "purchaseOrderType">
		  				<c:forEach items="${poTypeMap}" var="poTypeMap">
					        <option value="${poTypeMap.key}" <c:if test="${purchaseOrder.purchaseOrderType==poTypeMap.key}">selected</c:if>>
					        ${poTypeMap.value}
					        </option>
					    </c:forEach>		  				
				 		</form:select>
		  				<form:errors path="purchaseOrderType"  cssClass="errorSimple" /> 
						</aims:column>
						
							  				
		  				<c:if test="${purchaseOrder.accessDepartment.departmentId!=null}">
		  				<p class="span-11">
		  				<label>
	  					Access Department  
	 					</label> 					
	 					<c:out value="${purchaseOrder.accessDepartment.departmentName}"/>
		  				</p>
		  				</c:if>
		  				
		  				
		  					  				
		  				<c:if test="${purchaseOrder.purchaseOrderId!=null}">
						<p class="span-11">
						<label>
				  			PO Status 
				 		</label>
				 		<c:out value="${purchaseOrder.purchaseOrderStatus}" />
				 		</p> 
						</c:if>
						
		  				
		  				<p class="span-11">		  				
		  				
		  				<a class="button positive right" onclick="popUp()">
			  			 Map Asset to Vendor
			  			</a>
						</p>
											
			</div>
			
			</fieldset>
			
			<fieldset>
			
			<legend>Asset Info</legend>
			
			<div class="span-8 minLabel">
	  		<p class="clearfix">
	  		<label> Asset Model</label>
		  	<select name="assetModelId" id="assetModelId" class="assetModelId">
		  	</select>	
		  	</p>
		  	
		  	<p class="clearfix">
		  	<label> ETA Date</label>
	  	 	<input id="etaDate" name="purchaseOrderDetails.etaDate" type="text" onkeypress="return restrictCharacters(this, event, validChars);"/>
	  		<input id="etaDate_button" type="button" class="calendarBtn" />
		  	</p>
		  	
		  	<p class="clearfix">
		  	<label>Quantity </label> 
	  		<input name="quantityOrder" id= "quantityOrder" type="text" />
	  		</p>
	  	
		  	<p class="clearfix">
			  	<label>Unit price</label> 		
		  		<input name="unitPrice" id="unitPrice" type="text" value="" /> 	 	
		  	</p>
	  		
	  		<p class="clearfix">
	  		<a onclick="addRow()" class="button">+ Add to list</a>
	  		</p>	
			
			</div>
			
			<div class="span-14 last">	  		
	  	
	  	
		
		  	
	  		<table id="assetTable" class="gridSearch">
	  		<caption id="header" class="captionLeft">Total number of items : 
		  		<span id="totalItems"></span></caption>
	  			<thead>
	  				<tr>
	  				    <th>Remove</th>
	  					<th>Asset Model</th>
	  					<th>ETA Date</th>
	  					<th>Qty Order</th>
	  					<th>Qty Rcvd</th>
	  					<th>Price</th>
	  					<th>Total Price</th>
	  					
	  				</tr>
	  			</thead>
	  			
	  			<tbody>
	  			<c:forEach items="${purchaseOrder.purchaseOrderDetails}" var="item" varStatus="status">
	  				<tr <c:if test="${(status.count%2)==0 }"> class="alt"</c:if>>
	  					
	  						<input name="purchaseOrderDetails[${status.index}].purchaseOrderDetailId" type="hidden" value="${item.purchaseOrderDetailId}" />
	  						<input name="purchaseOrderDetails[${status.index}].deleted" type="hidden" value="${item.deleted}" class="deleted"/>
							<input name="purchaseOrderDetails[${status.index}].purchaseOrder.purchaseOrderId" type="hidden" value="${item.purchaseOrder.purchaseOrderId}" />
							<input name="purchaseOrderDetails[${status.index}].assetModel.assetModelId" type="hidden" class="assetModelId" value="${item.assetModel.assetModelId}"/> 
	  						<input name="purchaseOrderDetails[${status.index}].assetModel.assetModelName" type="hidden" class="assetModelName" value="${item.assetModel.assetModelName}"/>
							
							
	  					<td width="5%">
	  					
	  						<!--<a href="#" onclick="removeRow(this)">-</a>
	  						--><a href="javascript:void(0)" onclick="removeRow(this)" title="Delete Row">
	  						<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  					</a>
	  					
	  					</td>		  						
	  					
	  					<td width="30%">	  						
						
	  					<c:out value="${item.assetModel.assetModelName}" />
	  					
						</td>
						<td width="30%">
	  					<input id="etaDate_${status.index}" name="purchaseOrderDetails[${status.index}].etaDate" type="text" size="7" 
	  					onkeypress="return restrictCharacters(this, event, validChars);" value="<fmt:formatDate value="${item.etaDate}" pattern="${globalDateFormatJava}" />"/>
	  					<input id="etaDate_button_${status.index}" type="button" class="calendarBtn" />
	  					</td>
	  					<td width="13%">
	  					<input name="purchaseOrderDetails[${status.index}].quantityOrder" type="text" value="${item.quantityOrder}" size="2"
	  					class="quantityOrder"  onkeyup="calculateTotal(this)" />
	  					</td>
	  					<td width="12%">
	  					<input type="text" value="${item.quantityReceived}" size="2"
	  					class="quantityReceived" readonly="readonly" />
	  					</td>
	  					<td width="10%"> 
	  					<input name="purchaseOrderDetails[${status.index}].unitPrice" type="text" value="${item.unitPrice}" size="2"
	  					class="unitPrice"    onkeyup="calculateTotal(this)"/>	  					
	  					</td>
	  					
	  					<td width="10%">
	  					<input id="totalPrice_${status.index}" type="text" disabled="disabled" value="" size="2" class="totalPrice"/>
	  					</td>						 
	  					</tr>
	  					
	  			</c:forEach>
	  			</tbody>
	  		</table>
	  
	  		</div>
			
			</fieldset>
			
      <div class="action">
	  	<c:choose>
	  		<c:when test="${purchaseOrder.purchaseOrderId==null}">
	  			  			
	  			<button type="button" class="button positive" onclick="formSubmit()">
	  			Save
	  			</button>
	  			
	  			<a href="<c:url value="/purchaseOrder/create" />" class="button positive">Cancel</a>
	  			
	  			<a href="<c:url value="/purchaseOrder/search" />" class="button positive">Back</a>
	  		</c:when>
	  		<c:when test="${purchaseOrder.purchaseOrderId!=null}">
  				
  				<c:if test="${purchaseOrder.purchaseOrderStatus!=null && purchaseOrder.purchaseOrderStatus == newStatus}">
  				<button type="button" class="button positive" onclick="formUpdate()">
	  			Update
	  			</button>  
	  			</c:if>
	  			
	  			<c:if test="${purchaseOrder.purchaseOrderStatus!=null && ( purchaseOrder.purchaseOrderStatus == newStatus || purchaseOrder.purchaseOrderStatus == inprocessStatus )}">
	  			<a href="<c:url value="/purchaseOrder/close/${purchaseOrder.purchaseOrderId}" />" class="button positive">Close PO</a>
	  			</c:if>
	  			<a href="<c:url value="/purchaseOrder/print/${purchaseOrder.purchaseOrderId}" />" target="_blank" class="button">Print</a>
  				<button type="button" class="button positive" onclick="location.href='<c:url value="/purchaseOrder/search" />'">Back</button>
	  		</c:when>
	  	</c:choose>
	 </div>	
	
	
	
	<table id="tmpTable_1" style="visibility:hidden">
		<tr class="new">
		
		<input name="purchaseOrderDetails.purchaseOrderDetailId" type="hidden" />
		<input name="purchaseOrderDetails.deleted" type="hidden"  class="deleted"/>
		<input name="purchaseOrderDetails.purchaseOrder.purchaseOrderId" type="hidden" value="${purchaseOrder.purchaseOrderId}" />
		<input name="purchaseOrderDetails.assetModel.assetModelId" type="hidden" class="assetModelId" />
		<input name="purchaseOrderDetails.assetModel.assetModelName" type="hidden" />
		
		 <td width="5%"> 	
	  		<!--<a onclick="removeRow(this)">-</a>
	  		
	  		--><a href="javascript:void(0)" onclick="removeRow(this)" title="Delete Row">
	  						<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  					</a>
	  					
	  	</td>  	
	
	  	
	  	<td width="30%">	  	
	  	<span class="assetModelName"></span>	  	
	  	</td> 
	  				
	  <td width="30%">
	  	<input id="etaDate_" name="purchaseOrderDetails.etaDate" type="text" size="7" class="etaDate" onkeypress="return restrictCharacters(this, event, validChars);"/>
	  	<input id="etaDate_button_" type="button" class="calendarBtn" />
	  	</td>
	  	
	<td width="5%">
	  	<input name="purchaseOrderDetails.quantityOrder" type="text" size="2" 
	    onkeyup="calculateTotal(this)" class="quantityOrder" />
	  	</td>
		<td width="5%">
	  	<input name="quantityReceived" type="text" size="2"  readonly="readonly" value="0" />
	  	</td>  	
	  <td width="10%">
	  	<input name="purchaseOrderDetails.unitPrice" type="text" size="2"  
	  	onkeyup="calculateTotal(this)" class="unitPrice" />
	  	</td>	  	
	  	
	  	
	  	<td width="10%">
	  	<input id="totalPrice" type="text" size="2" class="totalPrice" disabled="disabled"/>
	  	</td>
	  	
		</tr>
	</table>
	
	</form:form>
	
	<script type="text/javascript" src="<spring:url value="/resources/js/select-option-disabled-emulation.js" />"></script>
	<script type="text/javascript" src="<spring:url value="/resources/js/restrictCharactersForDateInput.js" />"></script>
	<script type="text/javascript">
	
	var validChars = /[1234567890-]/g;
	
	
	var assetModelSet = new Array();
	var assetModelPurchaseOrder;
	var assetModelComp = new Array();
	var assetModelEquip = new Array();
	var finalValidRowCount = 0;
	var newwindow = null;
	
	
		$(document).ready(function() {			
			$(".unitPrice").numeric();
			$(".quantityOrder").numeric(false, function() { this.value = ""; this.focus(); });
			$("#unitPrice").numeric();
			$("#quantityOrder").numeric(false, function() { this.value = ""; this.focus(); });
			
			cal = Calendar.setup({
		          onSelect: function(cal) { cal.hide() },
		          showTime: false
		      });		
		
			
			var at = $('#assetTable tbody tr');
			if(at){
			finalValidRowCount = at.size();	
			var modelId;
			at.each(function () {	
				modelId = $('.assetModelId',this).val();
					assetModelSet.push(modelId);				
					}
				);
			}
				
			hideHiddenRows();
			
			if(finalValidRowCount<=0){
				$('#header').hide();				
			}
			else{			
				$('#header').show();
				$('#totalItems').text(finalValidRowCount);
			}
			
			rebuiltCalendar();
			checkTypeDropDown();
			setTotal();
			populateList();
			
		});
		
		function hideHiddenRows()
		{			
		var table = $(' #assetTable tbody tr');
			
			var hiddenRow=false;
			var modelId;
			table.each(
					function () {
						
					hiddenRow = $('.deleted',this).val();
					 modelId = $('.assetModelId',this).val();
					
					///alert(hiddenRow);
						if(hiddenRow=='true'){
							$(this).hide();
							finalValidRowCount--;
							removeInContain(assetModelSet,modelId);
						}
					}
					);
			
		}
		
		function setTotal()
		{
			
			var table = $(' #assetTable tbody tr');
			
			var unitPrice,quantityOrder;
			table.each(
					function () {					
						
					unitPrice = $('.unitPrice',this).val();
					quantityOrder = $('.quantityOrder',this).val();
					$('.totalPrice',this).val(unitPrice*quantityOrder);					
					}
					);
			
		}
		
		function redirect(form,url){
			form.attr('action',url );
			
			form.submit();
		}
		
		function formSubmit(){
			if(validateForm()){
			var url = "<c:url value="/purchaseOrder/create"/>";
			var form = $('#form');				
			redirect(form,url);
			}
		}
		
		
		function popUp(){
			var vendorId =  $('#vendorId').val();
			
			if(validate(vendorId)){			
				var url = "<c:url value="/vendor/updateAssets/"></c:url>"+vendorId+"?popUpFlag=Y";
				openPopUp(url);								
			}			
		}
		
		function validate(vendorId) {
			
			if(vendorId==''){
				alert('Pls do select vendor to map asset');
				return false;
			}
			
			return true;
		}
		
		 function openPopUp(url)  
		 {  
		  newwindow = window.open(url,'_blank',"height=500,width=1100,resizable=yes,scrollbars=yes");  
		  newwindow.focus() ;
		 } 
		 
		 function reloadParent()
		 {			 
			 setTimeout('refreshList()', 50);			
		 }
		 
		 function refreshList() {
			
			 if (!newwindow || newwindow.closed) {
				 populateList();
			 }
		 }
		
		function formUpdate(){
			if(validateForm()){
			var url = "<c:url value="/purchaseOrder/update/${purchaseOrder.purchaseOrderId}"/>";
			var form = $('#form');				
			redirect(form,url);
			}
		}
		
		
		function validateForm(){
			var res = true;
			
			if(finalValidRowCount<=0){
				alert(" Pls do update the details to start processing purchase order");
				res = false;
				return res;
			}
			
			
			var table = $(' #assetTable tbody tr');			
			var unitPrice;
			var quantityOrder;
			var etaDate;
			table.each(
					function (index) {
						
						
							
					if ( $(this).is(':visible') ) { 
							//alert('visible');
							
									  unitPrice = $('.unitPrice',this).val();
									  quantityOrder = $('.quantityOrder',this).val();
									  etaDate = $('.etaDate',this).val();					
									
									 if(etaDate == ''){
										alert(" Pls do enter the ETA Date");
										res = false;
										return false;
										}
									 
									 else if(quantityOrder == ''){
										alert("Pls do enter the qty ");
										res = false;
										return false;
										}
									 
									 else if(unitPrice==''){
										alert(" Pls do enter the price");
										res = false;
										return false;
										}
						}
						
					}
					);
			
			return res;
		}
		
				
		
		$('#assetType').change(function() {
			populateList();
			
			});
		
		$('.vendorId').change(function() {
			populateList();
			
			});
		
		$('.assetModelId').change(function() {
			populatePrice();			
			});
		
		
		
		$('.purchaseOrderType').change(function(){
			var purchaseOrderType =  $('#purchaseOrderType').val();
			if(purchaseOrderType ==  "REPAIR")
				{
				
				var url = "<c:url value="/purchaseOrder/creates"/>";
				var form = $('#form');				
				redirect(form,url);
							
				}
		});
		
		function redirect(form,url){
			
			form.attr('action',url );
			form.submit();
		}
		
		function populateList()
		{
						
			var assetType =  $('#assetType').val();
			var vendorId =  $('#vendorId').val();
			$('#assetModelId option').remove();
			if(assetType!="" && vendorId!=""){
					$.getJSON("<c:url value="/ajax/vendorPrice"></c:url>?vendorId="+vendorId+"&assetType="+assetType, function(data) {
						  
						  	 assetModelComp = new Array();
							 assetModelEquip = new Array();
								assetModelPurchaseOrder = data;
								
								for(i=0;i<data.length;i++)
								{
									if(assetModelPurchaseOrder[i].assetType == "COMPONENT"){
										assetModelComp.push(assetModelPurchaseOrder[i]);
										
									}
									else if(assetModelPurchaseOrder[i].assetType == "EQUIPMENT"){
										assetModelEquip.push(assetModelPurchaseOrder[i]);
									}
								}
								
								
								
								if(assetType == "COMPONENT"){
									  
									for(var i=0;i<assetModelComp.length;i++)
									{
										$('#assetModelId').
								          append($("<option></option>").
								          attr("value",assetModelComp[i].assetModelId).
								          text(assetModelComp[i].assetModelName));
										
									}
									 
								  }
								  
								  else if(assetType == "EQUIPMENT"){
									  for(var i=0;i<assetModelEquip.length;i++)
										{
											$('#assetModelId').
									          append($("<option></option>").
									          attr("value",assetModelEquip[i].assetModelId).
									          text(assetModelEquip[i].assetModelName));
											
										}
								  }
								
								populatePrice();
					});
			}
		}
		
		
		function populatePrice()
		{
			var index = ($("#assetModelId option").index($("#assetModelId option:selected")));
			
			if(index>=0)
				{
						var assetType =  $('#assetType').val();
						
						if(assetType == "COMPONENT"){
							$('#unitPrice').val(assetModelComp[index].unitPrice);
						}
						else if(assetType == "EQUIPMENT"){
							$('#unitPrice').val(assetModelEquip[index].unitPrice);
						}
				}

		}
				
		function rebuiltCalendar()
		{
			cal.manageFields("poDate_button", "poDate", globalJsCalDateFormat);
			cal.manageFields("etaDate_button", "etaDate", globalJsCalDateFormat);
			
			var assetTable = $('#assetTable tbody tr');
			if(assetTable)
			{
				for(var i=0;i<assetTable.size();i++)
					cal.manageFields("etaDate_button_"+i, "etaDate_"+i, globalJsCalDateFormat);
			}
			  
		}
		
		function checkContain(contain, obj)
		{
			if(contain && contain.length>0){
				for(var i=0;i<contain.length;i++){
					if(contain[i] == obj){
						return false;
					}
				}
			}
			contain.push(obj);
			return true;
		}
		
		function removeInContain(contain, obj)
		{
			if(contain && contain.length>0){
				for(var i=0;i<contain.length;i++){
					if(contain[i] == obj){
						//alert('position' +i);
						contain.splice(i,1);
					}
				}
			}
		}
		
		function addRow()
		{
			if(validateVendor())
			{
					var row = $('#tmpTable_1 tr').clone();
					var rowSize = $('#assetTable tbody tr').size();	
					var assetModelId = $(' #assetModelId').val();
										
				//	alert('rowSize' +rowSize	);
					
									
					if(checkContain(assetModelSet, assetModelId)){
					
							$('input[name=purchaseOrderDetails.purchaseOrderDetailId]',row).attr('name','purchaseOrderDetails['+rowSize+'].purchaseOrderDetailId');
							$('input[name=purchaseOrderDetails.purchaseOrder.purchaseOrderId]',row).attr('name','purchaseOrderDetails['+rowSize+'].purchaseOrder.purchaseOrderId');
							$('input[name=purchaseOrderDetails.deleted]',row).attr('name','purchaseOrderDetails['+rowSize+'].deleted').val(false);
							
							
							var assetModelName = $(' #assetModelId option:selected').text();
						
							var etaDate = $('#etaDate').val();
							var quantityOrder = $('#quantityOrder').val();
							var unitPrice = $('#unitPrice').val();
							
							var totalPrice = quantityOrder * unitPrice;
											
							//var select = $('select[name=purchaseOrderDetails.assetModel.assetModelId]',row).attr('name','purchaseOrderDetails['+rowSize+'].assetModel.assetModelId');
							//var option = $('#assetModelId option').clone();
							//option.appendTo(select);
							//select.val(assetModelId);
							
							$('input[name=purchaseOrderDetails.assetModel.assetModelId]',row).attr('name','purchaseOrderDetails['+rowSize+'].assetModel.assetModelId').val(assetModelId);
							$('input[name=purchaseOrderDetails.assetModel.assetModelName]',row).attr('name','purchaseOrderDetails['+rowSize+'].assetModel.assetModelName').val(assetModelName);
							
							$('.assetModelName',row).text(assetModelName);
							
							
							$('input[name=purchaseOrderDetails.etaDate]',row).attr('name','purchaseOrderDetails['+rowSize+'].etaDate').val(etaDate);
							$('#etaDate_',row).attr("id","etaDate_"+rowSize);
							$('#etaDate_button_',row).attr("id","etaDate_button_"+rowSize);
							$('input[name=purchaseOrderDetails.quantityOrder]',row).attr('name','purchaseOrderDetails['+rowSize+'].quantityOrder').val(quantityOrder);
							$('input[name=purchaseOrderDetails.unitPrice]',row).attr('name','purchaseOrderDetails['+rowSize+'].unitPrice').val(unitPrice);
							$('#totalPrice',row).attr("id","totalPrice_"+rowSize).val(totalPrice);
							
							
							row.appendTo('#assetTable tbody');
							cal.manageFields("etaDate_button_"+rowSize, "etaDate_"+rowSize, globalJsCalDateFormat);
							checkTypeDropDown();
							finalValidRowCount++;
							$('#totalItems').text(finalValidRowCount);
							$('#header').show();
							$(".unitPrice").numeric();
							$(".quantityOrder").numeric(false, function() { this.value = ""; this.focus(); });													
					
					}
					
					else{
						
						alert("The Asset has already add.");						
					}
			}
		}
		
		function validateVendor()
		{
			var vendorId =  $('#vendorId').val();
			if(vendorId==''){
				alert('Vendor is a mandatory field to start processing Purchase Order');
				return false;
			}
			var locationId = $('#locationId').val();
				if(locationId==''){
					alert('Location is a mandatory field to start processing Purchase Order');
					return false;
				}
				
				var assetModelId = $('#assetModelId').val();
				//alert(assetModelId);
				if(assetModelId=='' || assetModelId == null){
					alert('Valid Asset Model Code is needed to start processing Purchase Order');
					return false;
				}
				
		return true;
			
		}
		
		function removeRow(obj)
		{
			var ctx = $(obj).parent().parent();
			var modelId = $('input[name*=assetModelId]',ctx).val();
			
			
				$('input[name*=deleted]',ctx).val('true');
				ctx.hide();
			
			checkTypeDropDown();
			finalValidRowCount--;
			$('#totalItems').text(finalValidRowCount);
			$('#header').show();
			removeInContain(assetModelSet,modelId);
			
		}
		
		function checkTypeDropDown(){
			
			var assetTable = $('#assetTable tbody tr');
			var flag =  $('#flag').val();
			if(assetTable){
								
					if(assetTable.size()>0){
						$("#assetType option").not(":selected").attr("disabled",true);
						$("#purchaseOrderType option").not(":selected").attr("disabled",true);
						//if(flag == 'true'){
						$("#vendorId option").not(":selected").attr("disabled",true);
						$("#locationId option").not(":selected").attr("disabled",true);
						if(flag == 'true'){
						$("#companyCode option").not(":selected").attr("disabled",true);
						}
					  emulate(document.getElementById('vendorId'));
						emulate(document.getElementById('locationId'));
						//}
						
					}
					else{
						$("#assetType option").not(":selected").attr("disabled",false);
						$("#purchaseOrderType option").not(":selected").attr("disabled",false);
						//$("#companyCode option").not(":selected").attr("disabled",false);
											
					}
					
					emulate(document.getElementById('purchaseOrderType'));
					emulate(document.getElementById('assetType'));
					emulate(document.getElementById('companyCode'));
			}
		}
		function calculateTotal(obj) { 
					
			var row = $(obj).parent().parent();
			var unitPrice = $('.unitPrice',row);
			var quantityOrder = $('.quantityOrder',row);
			var totalPrice = $('.totalPrice',row);
			totalPrice.val(unitPrice.val()*quantityOrder.val());
						
		}
		
		
	</script>
